alert('ScenePlayerYoutube.js loaded');


var player;

function ScenePlayerYoutube() {

};
///////////////////////////////////////////////


////////////////////////////////////////////////
ScenePlayerYoutube.prototype.initialize = function () {
	alert("ScenePlayerYoutube.initialize()");
	// this function will be called only once when the scene manager show this scene first time
	// initialize the scene controls and styles, and initialize your variables here
	// scene HTML and CSS will be loaded before this function is called
};



ScenePlayerYoutube.prototype.handleShow = function (data) {
	alert("ScenePlayerYoutube.handleShow()");
	// this function will be called when the scene manager show this scene
	/*
	setTimeout(function(){
		sf.scene.hide('Player');
		sf.scene.show(''+NextScene+'');
		sf.scene.focus(''+NextScene+'');
		}, 60000);
	$("#VideoDiv video").bind("ended", function() {
		sf.scene.hide('Player');
		sf.scene.show(''+NextScene+'');
		sf.scene.focus(''+NextScene+'');
	});
	
	*
	*
	*		ytplayer.addEventListener("onStateChange", function(state){
	    if(state === 0){
	        // the video is end, do something here.
			sf.scene.hide('PlayerYoutube');
			sf.scene.show(''+NextScene+'');
			sf.scene.focus(''+NextScene+'');
	    }
		});
	*/


};

ScenePlayerYoutube.prototype.handleHide = function () {
	alert("ScenePlayerYoutube.handleHide()");
	// this function will be called when the scene manager hide this scene
};

ScenePlayerYoutube.prototype.handleFocus = function () {
	alert("ScenePlayerYoutube.handleFocus()");
	// this function will be called when the scene manager focus this scene
};

ScenePlayerYoutube.prototype.handleBlur = function () {
	alert("ScenePlayerYoutube.handleBlur()");
	// this function will be called when the scene manager move focus to another scene from this scene
};

ScenePlayerYoutube.prototype.handleKeyDown = function (keyCode) {
	alert("ScenePlayerYoutube.handleKeyDown(" + keyCode + ")");
	// TODO : write an key event handler when this scene get focued
	switch (keyCode) {
		case sf.key.LEFT:
			break;
		case sf.key.RIGHT:
			break;
		case sf.key.UP:
			break;
		case sf.key.DOWN:
			break;
		case sf.key.ENTER:
			alert("@@@@@@@@@@@@@@@@@@@@@@@@@ kikoooooooooooooooooooo");
			state = 0;
			PlayVideo = false;
			sf.scene.hide('PlayerYoutube');
			if(!NoHide){
				alert("+++++++++++++++++++++++++++++++++++++++++++++++ show");
				sf.scene.show(''+NextScene+'');
			}
			sf.scene.focus(''+NextScene+'');
			break;
		case sf.key.RETURN:			
			PlayVideo = false;
			event.preventDefault();
			sf.scene.hide('PlayerYoutube');
			sf.scene.show('Menu');
			sf.scene.focus('Menu');
			alert("plop2");
			break;
		default:
			alert("handle default key event, key code(" + keyCode + ")");
			break;
	}
};

function onYouTubePlayerReady() {
	
	alert("###### onYouTubePlayerReady " );
	player = document.getElementById('ytplayer');
	alert("player"+player);
	document.getElementById("showError").InnerHTML = "";
	
	onstate = function(state) {
		alert("%%%%% onStatechange "  + state);    
		
	    if(state === 0){
	        // the video is end, do something here.
	    	PlayVideo = false;
			sf.scene.hide('PlayerYoutube');
			sf.scene.show(''+NextScene+'');
			sf.scene.focus(''+NextScene+'');
	    }
	};

	onerror = function(code) {
		alert("$$$$$$$$$$ " + code);
	};

	player.addEventListener("onStateChange", "onstate");  
	player.addEventListener('onError','onerror');
	player.loadVideoById(VideoName);
	alert("###### Current Video Url " + player.getVideoUrl());
	alert("###### Current Video Embeded " + player.getVideoEmbedCode());
}

// tell samsung we're ready to go
var w = new Common.API.Widget();
w.sendReadyEvent();

